<template>
    <el-dialog class="add-budget" :title="`${editRows.length?'编辑报价单':'添加报价单'}`" :visible.sync="value" :before-close="close"  width="1000px">
        <el-form label-position="left" label-width="130px" :model="formData" :rules="formRules" ref="formRef">
            <el-form-item prop="project_demand_id" label="所属项目需求表">
                <el-select :disabled="preview" v-model="formData.project_demand_id">
                    <el-option v-for="(item,index) in demandList" :key="`demand-${index}`" :label="item.project.project_name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="budget_time" label="预算工期(天)">
                <el-input :disabled="preview" v-model.number="formData.budget_time" placeholder="预算工期(天)"></el-input>
            </el-form-item>
            <el-form-item prop="budget_money" label="预算总金额">
                <el-input-number :disabled="preview" style="width:300px" v-model="formData.budget_money" :controls="false" :precision="2" placeholder="预算总金额(元)"></el-input-number>
            </el-form-item>
            <el-form-item prop="budget_url" label="附件">
                <elUploadFile :disabled="preview" v-model="formData.budget_url" :multiple="false"></elUploadFile>
            </el-form-item>
            <el-form-item prop="desc" label="项目详情">
                <editor :preview="preview" v-model="formData.desc"></editor>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button type="primary" @click="submit">{{ editRows.length?('编辑'+(editRows.length>1?this.current+'/'+editRows.length:'')):'添加' }}</el-button>
            <el-button @click="close">关闭</el-button>
        </div>
    </el-dialog>
</template>
<script>
import elUploadFile from '@/components/el-upload-file.vue'
import editor from '@/components/editor.vue'
export default {
    data() {
        return {
            show:false,
            current:1,
            formData:{
                project_demand_id:'',
                budget_time:'',
                budget_money:undefined,
                budget_url:[],
                desc:'',
            },
            formRules:{
                project_demand_id:[
                    { required:true,message:'请选择所属项目需求表',trigger:['blur'] },
                ],
                budget_money:[
                    { required:true,message:'请输入预算总金额',trigger:['blur'] },
                ],
                budget_time:[
                    { required:true,message:'请输入预算工期(天)',trigger:['blur'] },
                ],
                budget_url:[
                    { required:true,message:'请上传附件',trigger:['blur'] },
                ],
                desc:[
                    { required:true,message:'请填写项目详情',trigger:['blur'] },
                ]
            },
            demandList:[]
        }
    },
    components:{
        elUploadFile,editor
    },
    props: {
        projectDemandId:Number,
        value: Boolean,
        editRows:{
            type:Array,
            default:()=>[]
        },
        preview:Boolean
    },
    watch:{
        projectDemandId(value){
            if(value){
                this.$emit('input',true)
                this.formData.project_demand_id = value
            }
        },
        editRows(value){
            if(value&&value.length){
                Object.assign(this.formData,this.editRows[0])
                this.formData.budget_url = [{
                    url:this.editRows[this.current-1].budget_url,
                    name:`附件`,
                    uid:Math.random().toFixed(10)
                }]
                this.$emit('input',true)
            }
        }
    },
    created(){
        this.getDemand()
    },
    methods: {
        async getDemand(){
            let res = await this.$get({
                url:'/admin/project.ProjectDemand/index',
            })
            this.demandList = res.list
        },
        submit(){
            this.$refs.formRef.validate(async(valid)=>{
                if(valid){
                    this.$loading()
                    // 编辑
                    let cpFormData = JSON.parse(JSON.stringify(this.formData)) // deep cp
                    cpFormData.budget_url = cpFormData.budget_url[0]?cpFormData.budget_url[0].url:'' //
                    if(this.editRows && this.editRows.length){
                        await this.$post({
                            url: "/admin/project.ProjectBudget/edit",
                            data: cpFormData,
                            loading: true,
                            successTip: true
                        })
                        if(this.current != this.editRows.length){
                            this.current += 1
                            Object.assign(this.formData,this.editRows[this.current-1])
                            return
                        }
                    }else{
                    //提交
                        await this.$post({
                            url: "/admin/project.ProjectBudget/add",
                            data: cpFormData,
                            loading: true,
                            successTip: true
                        })
                    }
                    this.close()
                    this.$emit('update')
                    this.$loading().close()
                }
            })
        },
        close(){
            Object.assign(this.$data.formData,this.$options.data().formData)
            this.current = 1
            this.$emit('input',false)
            this.$emit('update:projectDemandId',0)
            this.$emit('update:editRows',[])
            this.$emit('close')
            this.$emit('update:preview',false)
            setTimeout(() => {
                this.$refs.formRef.resetFields()
            }, 0);
        }
    }
}
</script>
<style>
.add-budget .el-input__inner{
    text-align: left;
}
</style>